package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/brand")
/**
 * 品牌Controller
 * fxq
 */
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 添加品牌
     * @param brand
     * @return
     * fxq
     */
    @PostMapping
    public Result add(@RequestBody Brand brand){
        try {
            brandService.add(brand);
            return new Result(true, StatusCode.OK,"添加品牌成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加品牌失败");
        }
    }

    /**
     * 分页条件查询品牌列表
     * @param page
     * @param size
     * @param condition
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable("page") int page, @PathVariable("size") int size, @RequestParam("condition") String condition){
        try {
            PageResult<Brand> brandList = brandService.findPage(page,size,condition);
            return new Result(true, StatusCode.OK,"查询品牌列表成功",brandList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询品牌列表失败");
        }
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        try {
            brandService.delete(id);
            return new Result(true,StatusCode.OK,"删除品牌成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR,"删除品牌失败");
    }


    /**
     * 根据商品分类id查询品牌下拉列表
     * @param id
     * @return
     */
    @GetMapping("/find/{id}")
    public Result findPage(@PathVariable int id) {
        List<String> brandNames = brandService.findBrandName(id);
        return new Result(true,StatusCode.OK,"查询成功",brandNames);
    }

}
